AWS EC2
参考
インスタンスの種類について
最近は3種類のEC2インスタンスに別れているみたい.
table: EC2違い
https://gyazo.com/3489f78447c8a955a3be762784b59503
無料枠のインスタンスを立ち上げる
インスタンスの作成
無料枠のAmazon Linux 2 AMI (HVM), SSD Volume Typeを選択
t2.microを選択
セキュリティグループまで行ってHTTPやHTTPSを追加しておく
他にも必要なポート開放があったらやっておく
確認と作成
キーペアがなければ作成してダウンロード,ローカルの~/.sshにでも置いておく
そしてインスタンスを作成
ダウンロードしたキーをchmod 400 new_keyでパーミッション変更
SSHでEC2インスタンスに接続
code: bash
ssh -i ~/.ssh/new_key.pem ec2-user@aaa.bbb.ccc.ddd
ElasticIPを設定する
インスタンスを再起動してもIPが変わらないようにElasticIPを設定する
ネットワーク&セキュリティ > Elastic IP
新しいアドレスの割り当て
作成したアドレスを選んで アクション>アドレスの関連付け
インスタンスとそのプライベートアドレスを選んで関連付ける
インスタンスを作り直す
ただ作り直して,同じキーペアを使うと,fingerprintのエラーが起きる
viでknown_hostsの当該IPの行を削除すればもう一度fingerprintを作り直せる
MacやLinuxならば以下のコマンドが正解
code: bash
$ ssh-keygen -R ec2-x-xx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
-Rオプションによってknown_hostsからec2の元のfingerprintを削除する
windowsではnot yet supportedらしいので直接削除する
Apacheの導入
code: bash
$ sudo yum -y install httpd
$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service # 自動起動
パブリックDSNにアクセスするとTestページが表示される.
code: bash
$ sudo chmod 755 /var
$ sudo chmod 755 /var/www
$ sudo chmod 755 /var/www/html
$ cd /var/www/html
$ echo "This is my test page" > index.html
として再度アクセスすると,This is my test pageと表示されることから,ここに静的ファイルを置くことでアクセスが可能になることが分かる.
Nginxの導入
NginxはApacheに比べて高速,高耐久,高並列性,省メモリなWebサーバである.
EC2ではnginxの入れ方が通常とは異なる部分があるので注意
code: bash
$ sudo amazon-linux-extras install nginx1.12 -y
$ nginx -v # 導入確認
$ sudo service httpd stop # apacheを止める
$ service httpd status # httpdがinactiveになっていることを確認
$ sudo systemctl disable httpd.service # httpdの自動起動を切る(Amazon Linux 2式)
$ sudo systemctl is-enabled httpd.service # httpdの自動起動が切れているか確認
$ sudo systemctl start nginx.service # nginx起動
$ systemctl status nginx # nginxがactiveになっていることを確認
$ curl -I localhost # 接続確認:200が返ってくれば成功
$ sudo systemctl enable nginx.service # nginxの自動起動を入れる
$ sudo systemctl is-enabled nginx.service # nginxの自動起動ができているか確認
node.js, yarnの導入
code: bash
$ sudo yum install -y gcc-c++ make
$ sudo yum install -y nodejs
$ sudo npm install -g n
$ sudo n latest
$ source .bash_profile
$ node --version
$ yarn --version
SSL証明書の作成
opensslを使う方法
まずEC2のセキュリティグループの設定で80ポートをHTTP, 443ポートをHTTPSで開けておく
秘密鍵と公開鍵のペアを作成し,SSL証明書の発行を行う.
code: bash
$ sudo yum install openssl openssl-devel
$ mkdir ~/openssl
$ cd ~/openssl
$ openssl genrsa 2048 > server.key # 証明書発行に使う秘密鍵
$ openssl req -new -key server.key > server.csr # 公開鍵 (challenging passwordはない方がいい?)
$ openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt # 証明書発行
これだけだと認証局の承認を得ていないため,オレオレ証明書になり,ブラウザからこの証明書を使ったアクセスにはユーザからの承認が必要になる. Let’s encryptを使う方法
前準備
インスタンス生成 & ElasticIP割り当て & ドメイン名紐づけ
HTTPSとHTTPのポートを開けておく
Apacheなどで何らかのページにhttpアクセスできるようにしておく
certbot導入
code: bash
$ sudo chmod 700 /usr/bin/certbot-auto
/usr/bin/cert-autoがAmazon Linux2に対応できるよう800何行目あたりにあるところを書き換え
code: cert-auto
Bootstrap() {
ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon
}
BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION"
↓
code: cert-auto
elif grep -i "Amazon Linux" /etc/issue > /dev/null 2>&1 || \
grep 'cpe:.*:amazon_linux:2' /etc/os-release > /dev/null 2>&1; then
Bootstrap() {
ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon
}
BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION"
鍵と証明書を発行する
code: bash
$ certbot-auto certonly --webroot -w /var/www/html -d test.example.com --email fuurin@gmail.com
certonly: 証明書の作成
--webroot: ウェブサーバーモードを指定し,現在HTTPでホスティングしているものを審査に使用. ⇔ standaloneモードは一旦サーバを止める必要があるが,certbotがホスティングを行ってくれる
-w /var/www/html ドキュメントルートのパス
flaskなら/home/ec2-user/my_flask_appのようにする
-d test.example.com 作成したドメイン名
--email fuurin@gmail.com 連絡用のメールアドレス.
鍵の期限が迫るとここにメールが来るらしい
galookのcertbotコマンド例
code:bash
sudo certbot-auto certonly --webroot -w /home/ec2-user/galook/api -d api.galook.net --email mii.informationsystems@gmail.com --debug
できた
code:text
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/test.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/text.example.com/privkey.pem
Your cert will expire on 2019-12-06. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- If you like Certbot, please consider supporting our work by:
主に以下の2つを使用する
/etc/letsencrypt/live/test.example.com/fullchain.pem
/etc/letsencrypt/live/test.example.com/privkey.pem
3カ月しかもたない.更新は次のように行う
code: bash
$ sudo certbot-auto renew --force-renew
code: bash
# cronが動作しているか確認
$ /etc/rc.d/init.d/crond status
$ sudo vi /etc/cron.d/letsencrypt
# 以下を書き込む
00 16 * * 2 root /usr/bin/certbot-auto renew --post-hook "service httpd restart"